package com.example.yuejian23.yuejian_23.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejian23.yuejian_23.Adapter.BisaiAdapter;
import com.example.yuejian23.yuejian_23.Adapter.GuandashiAdapter;
import com.example.yuejian23.yuejian_23.Adapter.GuanhuodongAdapter;
import com.example.yuejian23.yuejian_23.Adapter.GuanjulebuAdapter;
import com.example.yuejian23.yuejian_23.Adapter.TieziAdapter;
import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.activity.XiugaiActivity;
import com.example.yuejian23.yuejian_23.javaBean.BisaiBean;
import com.example.yuejian23.yuejian_23.javaBean.DashiBean;
import com.example.yuejian23.yuejian_23.javaBean.GerenBean;
import com.example.yuejian23.yuejian_23.javaBean.HuodongBean;
import com.example.yuejian23.yuejian_23.javaBean.JulebuBean;
import com.example.yuejian23.yuejian_23.javaBean.ShaituBean;
import com.example.yuejian23.yuejian_23.service.ProService;
import com.example.yuejian23.yuejian_23.service.UserService;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 王应予 on 2017/10/10.
 */

public class ProFragment extends Fragment
{
    Button btn_out;
    RecyclerView recyclerView,zhongxin;
    LinearLayout tiezi,shezhi,jiangpin,set_list,set_shezhi,dashiguan,julebuguan,huodongguan,bisaiguan;
    ImageView touxiang_thumb;
    TextView name;
    View view = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.tab05,container,false);

        initView();
        showData();
        dashi();
        return view;
    }

    private void initView() {
        //btn_out=(Button)view.findViewById(R.id.btn_out);
        recyclerView =(RecyclerView) view.findViewById(R.id.tie);
        recyclerView.setLayoutManager(new LinearLayoutManager(ProFragment.this.getContext()));
        zhongxin =(RecyclerView) view.findViewById(R.id.zhongxin);
        zhongxin.setLayoutManager(new LinearLayoutManager(ProFragment.this.getContext()));
        touxiang_thumb = (ImageView) view.findViewById(R.id.touxiang_thumb);
        name = (TextView) view.findViewById(R.id.name);

        tiezi = (LinearLayout) view.findViewById(R.id.tiezi);
        shezhi = (LinearLayout) view.findViewById(R.id.shezhi);
        jiangpin = (LinearLayout) view.findViewById(R.id.jiangpin);

        dashiguan = (LinearLayout) view.findViewById(R.id.dashiguan);
        julebuguan = (LinearLayout) view.findViewById(R.id.julebuguan);
        huodongguan=(LinearLayout)view.findViewById(R.id.huodongguan);
        bisaiguan=(LinearLayout)view.findViewById(R.id.bisaiguan);

        dashiguan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dashi();
            }
        });
        julebuguan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                julebu();
            }
        });
        huodongguan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                huodong();
            }


        });
        bisaiguan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bisai();
            }




        });

        

        set_list = (LinearLayout) view.findViewById(R.id.set_list);
        set_shezhi = (LinearLayout) view.findViewById(R.id.set_shezhi);


//        btn_out.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent=new Intent();
//                intent.setClass(.this,LoginActivity.class);
//                startActivityForResult(intent,1);
//
//            }
//        });

        tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                set_list.setVisibility(View.VISIBLE);
                set_shezhi.setVisibility(View.GONE);
                tiezi();

            }
        });
        shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                set_list.setVisibility(View.GONE);
                set_shezhi.setVisibility(View.VISIBLE);
                dashi();

            }
        });
    }


    private void showData()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        UserService userService=retrofit.create(UserService.class);

        Call<GerenBean> call = userService.getuserinfo(Mdata.uid);

        call.enqueue(new Callback<GerenBean>() {
            @Override
            public void onResponse(Call<GerenBean> call, Response<GerenBean> response) {
                //Toast.makeText(getActivity(), response.body().getUname(), Toast.LENGTH_SHORT).show();
                name.setText(response.body().getUname());
                Picasso.with(getActivity())
                        .load("http://115.159.93.244/new18yuejian/Public/upload/"+response.body().getUserpic())
                        .into(touxiang_thumb);
                touxiang_thumb.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent();
                        intent.setClass(getActivity(),XiugaiActivity.class);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(Call<GerenBean> call, Throwable t) {

            }
        });
    }
    private void tiezi()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ProService proService=retrofit.create(ProService.class);

        Call<List<ShaituBean>> call=proService.getList(Mdata.uid);
        //String.valueOf(2)
        call.enqueue(new Callback<List<ShaituBean>>() {
            @Override
            public void onResponse(Call<List<ShaituBean>> call, Response<List<ShaituBean>> response) {
                List<ShaituBean> home=response.body();

                recyclerView.setAdapter(new TieziAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<ShaituBean>> call, Throwable t) {

            }
        });

    }

    private void dashi()
    {

        int flag = 1;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ProService proService=retrofit.create(ProService.class);

        Call<List<DashiBean>> call=proService.getLists(Mdata.uid,flag);
        //String.valueOf(2)
        call.enqueue(new Callback<List<DashiBean>>() {
            @Override
            public void onResponse(Call<List<DashiBean>> call, Response<List<DashiBean>> response) {
                List<DashiBean> home=response.body();

                zhongxin.setAdapter(new GuandashiAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<DashiBean>> call, Throwable t) {

            }
        });

    }

    private void julebu()
    {

        int flag = 2;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ProService proService=retrofit.create(ProService.class);

        Call<List<JulebuBean>> call=proService.getListss(Mdata.uid,flag);
        //String.valueOf(2)
        call.enqueue(new Callback<List<JulebuBean>>() {
            @Override
            public void onResponse(Call<List<JulebuBean>> call, Response<List<JulebuBean>> response) {
                List<JulebuBean> home=response.body();

                zhongxin.setAdapter(new GuanjulebuAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<JulebuBean>> call, Throwable t) {

            }
        });

    }
    private void huodong() {
        int flag = 3;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ProService proService=retrofit.create(ProService.class);

        Call<List<HuodongBean>> call=proService.getListsss(Mdata.uid,flag);
        //String.valueOf(2)
        call.enqueue(new Callback<List<HuodongBean>>() {
            @Override
            public void onResponse(Call<List<HuodongBean>> call, Response<List<HuodongBean>> response) {
                List<HuodongBean> home=response.body();

                zhongxin.setAdapter(new GuanhuodongAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<HuodongBean>> call, Throwable t) {

            }
        });
    }
    private void bisai() {
        int flag = 4;
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ProService proService=retrofit.create(ProService.class);

        Call<List<BisaiBean>> call=proService.getListb(Mdata.uid,flag);
        //String.valueOf(2)
        call.enqueue(new Callback<List<BisaiBean>>() {
            @Override
            public void onResponse(Call<List<BisaiBean>> call, Response<List<BisaiBean>> response) {
                List<BisaiBean> home=response.body();

                zhongxin.setAdapter(new BisaiAdapter(getActivity(),home));

            }

            @Override
            public void onFailure(Call<List<BisaiBean>> call, Throwable t) {

            }
        });
    }
}
